package com.forfunnet.minjian.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.forfunnet.minjian.MyApplication;
import com.forfunnet.minjian.R;
import com.forfunnet.minjian.message.Ads;
import com.forfunnet.minjian.message.response.AdsListResponse;
import com.forfunnet.minjian.message.response.BaseResponse;
import com.forfunnet.minjian.ui.adapter.AdsImagePagerAdapter;
import com.viewpagerindicator.IconPageIndicator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

/**
 * Created by RexQian on 2/4/2015.
 */
@EViewGroup(R.layout.ads_banner_view)
public class AdsBannerView extends RelativeLayout {

    @App
    MyApplication mApp;

    @ViewById(R.id.view_pager)
    AutoScrollViewPager viewPager;

    AdsImagePagerAdapter mPagerAdapter;

    @ViewById(R.id.indicator)
    IconPageIndicator indicator;

    public AdsBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    float mAttrRatio;

    private void initialise( AttributeSet attrs )
    {
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.AdsBannerView);
        mAttrRatio = a.getFloat(R.styleable.AdsBannerView_ratio, 0.5f);
        a.recycle();
    }

    @AfterViews
    void afterViews()
    {
    }

    public void stop()
    {
        viewPager.stopAutoScroll();
    }

    public void start()
    {
        getData();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec((int)(widthSize * mAttrRatio),
                MeasureSpec.AT_MOST);

        getLayoutParams().width = widthSize;
        getLayoutParams().height = (int)(widthSize * mAttrRatio);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Background
    void getData ()
    {
        AdsListResponse resp = mApp.restClient().getAdsList();
        getDataResult(resp);
    }

    @UiThread
    void getDataResult (AdsListResponse resp)
    {
        if(BaseResponse.hasError(resp))
            return;

        List<AdsImagePagerAdapter.ItemData> dataList
                = new ArrayList<AdsImagePagerAdapter.ItemData>();
        for (int i = 0; i < resp.DataList.size(); i++)
        {
            AdsImagePagerAdapter.ItemData data = new AdsImagePagerAdapter.ItemData();
            Ads ads = resp.DataList.get(i);
            data.mImageUrl = String.format("%s/imageAds/%d/image.json",
                    mApp.restClient().getRootUrl(), ads.Id);
            data.mUrl = ads.Url;
            data.mContent = ads.Content;
            dataList.add(data);
        }

        mPagerAdapter = new AdsImagePagerAdapter(getContext(), dataList);
        mPagerAdapter.setImageDestSize(getLayoutParams().width, getLayoutParams().height);
        mPagerAdapter.setOnAdsClickListener(mAdsClickListener);
        viewPager.setAdapter(mPagerAdapter);
        indicator.setViewPager(viewPager);
        if (dataList.size() > 1) {
            indicator.setVisibility(View.VISIBLE);
            indicator.notifyDataSetChanged();
        }
        else {
            indicator.setVisibility(View.INVISIBLE);
        }
        viewPager.setCycle(true);
        viewPager.setInterval(3000);
        viewPager.startAutoScroll();
    }

    private AdsImagePagerAdapter.OnAdsClickListener mAdsClickListener;
    public void setOnAdsClickListener(AdsImagePagerAdapter.OnAdsClickListener listener)
    {
        mAdsClickListener = listener;
        if(mPagerAdapter != null)
            mPagerAdapter.setOnAdsClickListener(mAdsClickListener);
    }
}
